package com.kaifamiao.provider;

import com.kaifamiao.model.UserModel;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
import org.apache.ibatis.jdbc.SQL;

public class UserProvider implements ProviderMethodResolver {

    public static String selectAll(final UserModel model){
        return new SQL(){{
            SELECT("*");
            FROM("user");
            if(model!=null){
                if(model.getId()!=null){
                    WHERE("id=#{id}");
                }
                if(model.getAccountname()!=null){
                    WHERE("accountname=#{accountname}");
                }
                if(model.getName()!=null){
                    WHERE("name=#{name}");
                }
                if(model.getLogtimes()!=null){
                    WHERE("logtimes=#{logtimes}");
                }
                if(model.getSex()!=null){
                    WHERE("sex=#{sex}");
                }
                if(model.getTouxiang()!=null){
                    WHERE("touxiang=#{touxiang}");
                }
                if(model.getName()!=null){
                    WHERE("name=#{name}");
                }
                if(model.getRegdate()!=null){
                    WHERE("regdate=#{regdate}");
                }
                if(model.getPhone()!=null){
                    WHERE("phone=#{phone}");
                }
                if(model.getRoleId()!=null){
                    WHERE("roleId=#{roleId}");
                }
            }
        }}.toString();
    }

    public static String selectByPrimaryKey(final UserModel model){
        return new SQL(){{
            SELECT("*");
            FROM("user");
            if(model!=null) {
                if (model.getId() != null) {
                    WHERE("id=#{id}");
                }
            }
        }}.toString();
    }

    public  static String insertSelective(final UserModel model){
        SQL sql=new SQL().INSERT_INTO("user");
        if(model!=null){
            if(model.getId()!=null){
                sql.VALUES("id","#{id}");
            }
            if(model.getAccountname()!=null){
                sql.VALUES("accountname","#{accountname}");
            }
            if(model.getPassword()!=null){
                sql.VALUES("password","#{password}");
            }
            if(model.getName()!=null){
                sql.VALUES("name","#{name}");
            }
            if(model.getLogtimes()!=null){
                sql.VALUES("logtimes","#{logtimes}");
            }
            if(model.getSex()!=null){
                sql.VALUES("sex","#{sex}");
            }
            if(model.getTouxiang()!=null){
                sql.VALUES("touxiang","#{touxiang}");
            }
            if(model.getRegdate()!=null){
                sql.VALUES("regdate","#{regdate}");
            }
            if(model.getPhone()!=null){
                sql.VALUES("phone","#{phone}");
            }
            if(model.getRoleId()!=null){
                sql.VALUES("roleId","#{roleId}");
            }
        }
        return sql.toString();
    }
    public static String updateByPrimaryKeySelective(final UserModel model){
        return new SQL(){{
            UPDATE("user");
            if(model!=null){
                if(model.getAccountname()!=null){
                    SET("accountname=#{accountname}");
                }
                if(model.getPassword()!=null){
                    SET("password=#{password}");
                }
                if(model.getName()!=null){
                    SET("name=#{name}");
                }
                if(model.getLogtimes()!=null){
                    SET("logtimes=#{logtimes}");
                }
                if(model.getSex()!=null){
                    SET("sex=#{sex}");
                }
                if(model.getTouxiang()!=null){
                    SET("touxiang=#{touxiang}");
                }
                if(model.getName()!=null){
                    SET("name=#{name}");
                }
                if(model.getRegdate()!=null){
                    SET("regdate=#{regdate}");
                }
                if(model.getPhone()!=null){
                    SET("phone=#{phone}");
                }
                if(model.getRoleId()!=null){
                    SET("roleId=#{roleId}");
                }
                if(model.getId()!=null){
                    WHERE("id=#{id}");
                }
            }

        }}.toString();
    }
    public static String deleteByPrimaryKey(final UserModel model){
        return new SQL(){{
            DELETE_FROM("user");
            if(model!=null){
                if(model.getId()!=null){
                    WHERE("id=#{id}");
                }
            }
        }}.toString();
    }

    public static String selectByUser(final UserModel model) {
        return new SQL() {{
            SELECT("*");
            FROM("user");
            if (model != null) {
                if(model.getAccountname()!=null){
                    WHERE("accountname=#{accountname}");
                }
                if(model.getPassword()!=null){
                    WHERE("password=#{password}");
                }
            }
        }}.toString();
    }

    public static String selectByAccountName(final String accountname){
        return new SQL(){{
            SELECT("*");
            FROM("user");
            if(accountname != null){
                WHERE("accountname=#{accountname}");
            }
        }}.toString();
    }
}
